import requests
from loguru import logger
from copy import deepcopy
import uuid

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}


class ProdApi:
    session = requests.Session()

    # 接口1.5
    @staticmethod
    def listPageUpdate(data: dict):
        """
        更新列表上传ext
        :param data:
            必要字段: id | reviewName | level | orgId | nodeType | url
        """
        url = 'https://www.wpooldata.com.cn/reviewapi/api-j/openapi/updatePageUrl'

        localHeader = deepcopy(headers)
        localHeader.update({'Content-Type': 'application/json'})

        try:
            data.pop('pageId')
            response = ProdApi.session.post(url, headers=localHeader, json=data)
            logger.info(response.json())
            response.close()
        except Exception as e:
            logger.warning(e)

    # 接口2
    @staticmethod
    def getList(cityCode, countryCode, orgId=None, extStatus=None):
        """
        获取列表页链接
        :param cityCode:
        :param countryCode:
        :param orgId:
        :param extStatus: 有效值为'N'
        :return:
        """
        url = 'https://www.wpooldata.com.cn/reviewapi/api-j/openapi/reviewPage'

        params = {
            'cityId': cityCode,
            'countyId': countryCode,
            'pageSize': 1000,
        }
        if orgId:
            params.update({'orgId': orgId})
        if extStatus:
            params.update({'extStatus': extStatus})

        try:
            response = ProdApi.session.get(url, params=params, headers=headers)
            result = response.json()
            response.close()
            return result
        except Exception as e:
            logger.warning(e)

    # 接口3
    @staticmethod
    def listPageUrlPost(body: list):
        """
        :param body: list of dict
            必要字段: pageId, url, title
            非必要字段: pushTime, pushOrgName
        :return:
        """
        url = 'https://www.wpooldata.com.cn/reviewapi/api-j/openapi/saveUrl'

        localHeader = deepcopy(headers)
        localHeader.update({'Content-Type': 'application/json'})
        try:
            response = ProdApi.session.post(url, headers=localHeader, json=body)
            logger.info(response.json())
            response.close()
        except Exception as e:
            logger.warning(e)

    # 接口4
    @staticmethod
    def listEndPost(pageId, num=None, remark=None):
        url = 'https://www.wpooldata.com.cn/reviewapi/api-j/openapi/saveEnd'

        body = {
            'id': pageId
        }
        if num:
            body.update({'num': num})
        if remark:
            body.update({'remark': remark})

        localHeader = deepcopy(headers)
        localHeader.update({'Content-Type': 'application/json'})
        try:
            response = ProdApi.session.post(url, json=body, headers=localHeader)
            logger.info(response.json())
            response.close()
        except Exception as e:
            logger.warning(e)

    # 接口5
    @staticmethod
    def detailUrlGet(cityId, countyCode, pageSize=20, orgId=None, dataId=None):
        url = 'https://www.wpooldata.com.cn/reviewapi/api-j/openapi/url'

        params = {
            'cityId': cityId,
            'countyId': countyCode,
            'pageSize': pageSize,
            'uuid': str(uuid.uuid4())
        }

        if orgId:
            params.update({'orgId': orgId})
        if dataId:
            params.update({'dataId': dataId})

        try:
            response = requests.get(url, headers=headers, params=params)
            return response.json()
        except Exception as e:
            logger.warning(e)

    # 接口6
    @staticmethod
    def detailContentSave(data: dict):
        url = 'https://www.wpooldata.com.cn/reviewapi/api-j/openapi/content'
        localHeader = deepcopy(headers)
        localHeader.update({'Content-Type': 'application/json'})

        try:
            response = requests.post(url, headers=localHeader, json=data)
            logger.info(response.json())
        except Exception as e:
            logger.error(e)


class TestApi:
    # 接口1.5
    @staticmethod
    def listPageUpdate(data: dict):
        """
        更新列表上传ext
        :param data:
            必要字段: id | reviewName | level | orgId | nodeType | url
        """
        url = 'https://test.wpooldata.com.cn/reviewtestapi/api-j/openapi/updatePageUrl'

        localHeader = deepcopy(headers)
        localHeader.update({'Content-Type': 'application/json'})

        try:
            data.pop('pageId')
            response = requests.post(url, headers=localHeader, json=data)
            logger.info(response.json())
        except Exception as e:
            logger.warning(e)

    # 接口2
    @staticmethod
    def getList(cityCode, countryCode, orgId=None, extStatus=None):
        """
        获取列表页链接
        :param cityCode:
        :param countryCode:
        :param orgId:
        :param extStatus: 有效值为'N'
        :return:
        """
        url = 'https://test.wpooldata.com.cn/reviewtestapi/api-j/openapi/reviewPage'

        params = {
            'cityId': cityCode,
            'countyId': countryCode,
            'pageSize': 1000,
        }
        if orgId:
            params.update({'orgId': orgId})
        if extStatus:
            params.update({'extStatus': extStatus})

        try:
            response = requests.get(url, params=params, headers=headers)
            return response.json()
        except Exception as e:
            logger.warning(e)

    # 接口3
    @staticmethod
    def listPageUrlPost(body: list):
        """
        :param body: list of dict
            必要字段: pageId, url, title
            非必要字段: pushTime, pushOrgName
        :return:
        """
        url = 'https://test.wpooldata.com.cn/reviewtestapi/api-j/openapi/saveUrl'

        localHeader = deepcopy(headers)
        localHeader.update({'Content-Type': 'application/json'})
        try:
            response = requests.post(url, headers=localHeader, json=body)
            logger.info(response.json())
        except Exception as e:
            logger.warning(e)

    # 接口4
    @staticmethod
    def listEndPost(pageId, num=None, remark=None):
        url = 'https://test.wpooldata.com.cn/reviewtestapi/api-j/openapi/saveEnd'

        body = {
            'id': pageId
        }
        if num:
            body.update({'num': num})
        if remark:
            body.update({'remark': remark})

        localHeader = deepcopy(headers)
        localHeader.update({'Content-Type': 'application/json'})
        try:
            response = requests.post(url, json=body, headers=localHeader)
            logger.info(response.json())
        except Exception as e:
            logger.warning(e)

    # 接口5

    @staticmethod
    def detailUrlGet(cityId, countyCode, pageSize=20):
        url = 'https://test.wpooldata.com.cn/reviewtestapi/api-j/openapi/url'

        params = {
            'cityId': cityId,
            'countyId': countyCode,
            'pageSize': pageSize
        }

        try:
            response = requests.get(url, headers=headers, params=params)
            return response.json()
        except Exception as e:
            logger.warning(e)

    # 接口6
    @staticmethod
    def detailContentSave(data: dict):
        url = 'https://test.wpooldata.com.cn/reviewtestapi/api-j/openapi/content'
        localHeader = deepcopy(headers)
        localHeader.update({'Content-Type': 'application/json'})

        try:
            response = requests.post(url, headers=localHeader, json=data)
            logger.info(response.json())
        except Exception as e:
            logger.error(e)


if __name__ == '__main__':
    # Api.getList(370100, 370101)
    # TestApi.detailUrlGet('370100', '370101', 100)
    ProdApi.getList('370100', '370101')
